<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入用户名" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="选择用户类型" clearable style="width: 200px">
          <el-option label="个人" :value="1" />
          <el-option label="商家" :value="2" />
          <el-option label="公信部门" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增用户</el-button>
      </el-col>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="userList" row-key="id">
      <el-table-column prop="username" label="用户名" width="150"></el-table-column>
      <el-table-column prop="userType" label="用户类型" width="150">
        <template #default="scope">
          <span>{{ getUserTypeLabel(scope.row.userType) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="location" label="常在位置" width="150"></el-table-column>
      <el-table-column prop="realTimeLocation" label="实时位置" width="150"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="150"></el-table-column>
      <el-table-column prop="balance" label="账户余额" width="120">
        <template #default="scope">
          <span>{{ scope.row.balance }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="创建时间" width="200"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="150"></el-table-column>
      <el-table-column prop="status" label="账号状态" width="120">
        <template #default="scope">
          <span>{{ getStatusLabel(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="userRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="form.username" placeholder="请输入用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password" placeholder="请输入密码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户类型" prop="userType">
              <el-select v-model="form.userType" placeholder="选择用户类型">
                <el-option label="个人" :value="1" />
                <el-option label="商家" :value="2" />
                <el-option label="公信部门" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在地" prop="location">
              <el-input v-model="form.location" placeholder="请输入所在地" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="contactInfo">
              <el-input v-model="form.contactInfo" placeholder="请输入联系方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账户余额" prop="balance">
              <el-input v-model="form.balance" type="number" placeholder="请输入账户余额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="选择用户状态">
                <el-option label="正常" value="正常" />
                <el-option label="禁用" value="禁用" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="头像" prop="avatar">
              <el-input v-model="form.avatar" placeholder="请输入头像链接" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="实名认证信息" prop="realNameInfo">
              <el-input v-model="form.realNameInfo" placeholder="请输入实名认证信息" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="UserList">
import { ref, reactive } from 'vue';
import { listUsers, addUser, updateUser, deleteUser } from "@/api/pages/user";

const loading = ref(true);
const open = ref(false);
const showSearch = ref(true);
const title = ref("");
const refreshTable = ref(true);
const userList = ref([]);
const form = reactive({
  id: undefined,
  username: "",
  password: "",
  userType: 1,
  location: "",
  contactInfo: "",
  balance: 0.00,
  status: "正常",
  avatar: "",
  realNameInfo: ""
});
const queryParams = reactive({
  username: "",
  userType: undefined
});
const rules = {
  username: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
  password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
  userType: [{ required: true, message: "用户类型不能为空", trigger: "change" }],
};

// 查询用户列表
const getList = () => {
  loading.value = true;
  listUsers(queryParams).then(response => {
    userList.value = response.data;
    loading.value = false;
  });
};

// 取消按钮
const cancel = () => {
  open.value = false;
  reset();
};

// 重置表单
const reset = () => {
  Object.assign(form, {
    id: undefined,
    username: "",
    password: "",
    userType: 1,
    location: "",
    contactInfo: "",
    balance: 0.00,
    status: "正常",
    avatar: "",
    realNameInfo: ""
  });
};

// 搜索按钮操作
const handleQuery = () => {
  getList();
};

// 重置按钮操作
const resetQuery = () => {
  Object.assign(queryParams, {
    username: "",
    userType: undefined
  });
  getList();
};

// 新增按钮操作
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "新增用户";
};

// 修改按钮操作
const handleUpdate = (row) => {
  Object.assign(form, row);
  open.value = true;
  title.value = "修改用户";
};

// 提交按钮
const submitForm = () => {
  const apiCall = form.id ? updateUser : addUser;
  apiCall(form).then(() => {
    getList();
    open.value = false;
  });
};

// 删除按钮操作
const handleDelete = (row) => {
  deleteUser(row.id).then(() => {
    getList();
  });
};

// 获取用户类型标签
const getUserTypeLabel = (userType) => {
  switch (userType) {
    case 1: return '个人';
    case 2: return '商家';
    case 3: return '公信部门';
    default: return '';
  }
};
// 获取用户类型标签
const getStatusLabel = (status) => {
  switch (status) {
    case 1: return '正常';
    case 2: return '冻结';
    default: return '';
  }
};


// 初始化用户列表
getList();
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
.dialog-footer {
  text-align: right;
}
</style>